﻿using System;
using Results.Models;

namespace Results.ViewModels
{
    /// <summary>
    /// Statistics for a given player's seasons results within a specific group
    /// </summary>
    public class PlayerSeasonViewModel : ViewModelBase
    {
        String _season;
        public String Season
        {
            get
            {
                return _season;
            }
            set
            {
                if (_season != value)
                {
                    _season = value;
                    Notify("Season");
                }
            }
        }

        String _group;
        public String Group
        {
            get
            {
                return _group;
            }
            set
            {
                if (_group != value)
                {
                    _group = value;
                    Notify("Group");
                }
            }
        }

        String _average;
        public String Average
        {
            get
            {
                return _average;
            }
            set
            {
                if (_average != value)
                {
                    _average = value;
                    Notify("Average");
                }
            }
        }

        String _lowest;
        public String Lowest
        {
            get
            {
                return _lowest;
            }
            set
            {
                if (_lowest != value)
                {
                    _lowest = value;
                    Notify("Lowest");
                }
            }
        }

        String _points;
        public String Points
        {
            get
            {
                return _points;
            }
            set
            {
                if (_points != value)
                {
                    _points = value;
                    Notify("Points");
                }
            }
        }

        int _total;
        int _count;
        int _min;
        int _pts;

        /// <summary>
        /// This is the way to build the statistics... add results associated with this player, season, group
        /// </summary>
        /// <param name="resultModel"></param>
        public void AddResult(ResultModel resultModel)
        {
            AddRound(resultModel.Round1);
            AddRound(resultModel.Round2);

            try
            {
                var points = Convert.ToInt32(resultModel.Points);
                _pts += points;
                Points = _pts.ToString();
            }
            catch (Exception)
            {
            }
        }

        void AddRound(string round)
        {
            try
            {
                if (round.Length > 0 && char.IsDigit(round[0]))
                {
                    var strokes = Convert.ToInt32(round);
                    if (strokes > 0)
                    {
                        _total += strokes;
                        _count++;
                        if (_count == 1 || strokes < _min)
                        {
                            _min = strokes;
                            Lowest = _min.ToString();
                        }
                        Average = ((double)_total / _count).ToString("0.0");
                    }
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
